package com.ds.example.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiUserGetuserinfoRequest;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.response.OapiUserGetuserinfoResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.ds.common.result.Result;
import com.ds.example.service.DsUserService;
import com.ds.model.model.DsUserEntity;
import com.ds.model.vo.LoginVo;
import com.ds.model.vo.SysUserQueryVo;
import com.taobao.api.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 *
 *
 * @author ds
 * @email DS@gmail.com
 * @date 2023-05-15 09:48:17
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("admin/dingding/dsuser")
public class DsUserController {

    @Autowired
    private DsUserService dsUserService;

    /**
     * 列表
     */
    @ApiOperation("用户条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        SysUserQueryVo sysUserQueryVo){

        //创建page对象
        Page<DsUserEntity> pageParam = new Page<>(page,limit);

        //封装条件，判断条件值不为空
        LambdaQueryWrapper<DsUserEntity> wrapper = new LambdaQueryWrapper<>();
        //获取条件值
        String username = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        //判断条件值不为空
        //like 模糊查询
        if(!StringUtils.isEmpty(username)) {
            wrapper.like(DsUserEntity::getUsername,username);
        }
        //ge 大于等于
        if(!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.ge(DsUserEntity::getCreateTime,createTimeBegin);
        }
        //le 小于等于
        if(!StringUtils.isEmpty(createTimeEnd)) {
            wrapper.le(DsUserEntity::getCreateTime,createTimeEnd);
        }

        //调用mp的方法实现条件分页查询
        IPage<DsUserEntity> pageModel = dsUserService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }


    /**
     * 信息
     */
    @ApiOperation("根据id获取")
    @GetMapping ("/info/{id}")
    public Result info(@PathVariable("id") Integer id){
		DsUserEntity dsUser = dsUserService.getById(id);

        return Result.ok(dsUser);
    }

    /**
     * 保存
     */
    @ApiOperation("保存用户信息")
    @PostMapping("/save")
    public Result save(@RequestBody DsUserEntity dsUser){
		dsUserService.save(dsUser);

        return Result.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改用户信息")
    @PostMapping("/update")
    public Result update(@RequestBody DsUserEntity dsUser){
		dsUserService.updateById(dsUser);

        return Result.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除用户信息")
    @PostMapping("/delete")
    public Result delete(@RequestBody Integer[] ids){
		dsUserService.removeByIds(Arrays.asList(ids));

        return Result.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除用户信息")
    @GetMapping("/deleteById")
    public Result deleteById(@RequestBody Integer id){
        dsUserService.removeById(id);

        return Result.ok();
    }

}
